<?php
namespace app\api\service;
use think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Product extends Controller {

    /**
     * 获取产品列表
     * @param $param
     * @return mixed
     */
    public static function get_product_list($param) {
        try {
            if (!$data = cache('product_list_' . $param['category_id'])) {
                $cate = model('Category')->get(['id' => $param['category_id']]);
                $category = model('Category')->get(['id' => $cate['pid']]);

                $list = model('Category')->all(['pid' => $cate->pid]);
                if ($list) {
                    foreach ($list as $k => $v) {
                        $list[$k]['image'] = get_img_url($v['image']);

                        $product_list =  model('Product')->all(['category_id' => $v['id'], 'delete' => 0, 'status' => 2]);
                        foreach ($product_list as $key => $val) {
                            $product_list[$key]['thumb'] = get_img_url($val['thumb']);
                        }
                        $list[$k]['product_list'] = $product_list;
                    }
                }
                $data = ['list' => $list, 'category' => $category];
                //cache('product_list_' . $param['category_id'], $data);
            }
            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 获取产品详情
     * @param $param
     * @return mixed
     */
    public static function get_product_info($param) {
        if (!$data = cache('product_info_' . $param['id'])) {
            $data = model('Product')->get(['id' => $param['id'], 'delete' => 0, 'status' => 2]);
            if ($data) {
                // 商品详情
                $data->content = $data->productDetails->content;

                // 商品图片
                $images = $data->images()->where('delete', 0)->order('is_main desc')->select();
                foreach ($images as $key => $val) {
                    $images[$key]['image'] = get_img_url($val['image']);
                }
                $data->image = $images;

                // 商品属性
                $attr = model('Attribute')->where('id', 'in', $data['attr'])->select();
                $attr_value = model('AttrValue')->where('id', 'in', $data['attr_value'])->select();
                foreach ($attr as $key => $val) {
                    $val['image'] = get_img_url($val['image']);
                    $attr_v = [];
                    foreach ($attr_value as $k => $v) {
                        if ($v['attribute_id'] == $val['id']) {
                            $v['image'] = get_img_url($v['image']);
                            $attr_v[] = $v;
                        }
                    }
                    if ($attr_v) {
                        $val['value'] = $attr_v;
                    }
                    $attr[$key] = $val;
                }
                $data->attr = $attr;
            }
            cache('product_info_' . $param['id'], $data);
        }
        return api_return('200', $data);
    }
}